package com.example.demo.controller;

import com.example.demo.entity.UserProductinfo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.lang.reflect.Field;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Util {

    public static Map<String,Object> getJsonStr(String appId, String model) throws JsonProcessingException {
        Map<String,Object> infoList=new HashMap<>();
        infoList.put("categoryId","10169");
        infoList.put("categoryName","办公文档");
        infoList.put("categoryLinkName","文档-办公文档");
        infoList.put("categoryFullName","文档-办公文档");
        infoList.put("categoryParentId","0");
        infoList.put("categoryLevel",1);
        infoList.put("categoryCode","11000000");
        infoList.put("categoryCodeUseId",61);
        infoList.put("categoryTypeId",3);
        infoList.put("externalCode","61.11000000."+model);
        infoList.put("encodeTypeName","MA");
        List<Map<String, String>> newList = new ArrayList<>();
        Map<String, String> newItem = new HashMap<>();
        if(appId.equals("purchaseOrder")){
            infoList.put("modelName","采购申请");
            String[] originalList={"申请部门","申请人","联系电话","电子邮箱","物品信息","规格型号","数量","预计单价","总预算",
                    "采购目的","分配计划","交货时间","安装要求","验收标准"};
            newItem.put("attributeName", "采购申请");
            newItem.put("attributeValue", "申请部门,申请人,联系电话,电子邮箱,物品信息,规格型号,数量,预计单价,总预算," +
                    "采购目的,分配计划,交货时间,安装要求,验收标准");
            newList.add(newItem);
        }else if(appId.equals("order")){
            infoList.put("modelName","采购订单");
            newItem.put("attributeName", "订单信息");
            newItem.put("attributeValue", "订单编号,下单日期,产品型号,单价,数量,总价");
            Map<String, String> newItem2 = new HashMap<>();
            newItem2.put("attributeName", "采购方信息");
            newItem2.put("attributeValue", "公司名称,地址,联系人,联系电话,电子邮箱,交货时间,安装要求,验收标准");
            Map<String, String> newItem3 = new HashMap<>();
            newItem3.put("attributeName", "供应方信息");
            newItem3.put("attributeValue", "公司名称,地址,联系人,联系电话,电子邮箱");
            newList.add(newItem);
            newList.add(newItem2);
            newList.add(newItem3);
        }else if(appId.equals("logisticsOrder")){
            infoList.put("modelName","物流订单");
            newItem.put("attributeName", "物流信息");
            newItem.put("attributeValue", "订单编号,订单标识,物流单号,发货人,发货人电话,发货地址,收件人,收货人电话,收货地址,物品信息,物品数量");
            newList.add(newItem);
        }else if(appId.equals("materialPlan")){
            infoList.put("modelName","物料单");
            newItem.put("attributeName", "物料信息");
            newItem.put("attributeValue", "物料列表");
            newList.add(newItem);
        }else if(appId.equals("producePlan")){
            infoList.put("modelName","排产计划");
            newItem.put("attributeName", "排产信息");
            newItem.put("attributeValue", "计划编号,订单编号,接单日期,产品型号,订单数量,客户名称,排产日期,完成日期,加工车间");
            newList.add(newItem);
        }else if(appId.equals("dispatchOrder")){
            infoList.put("modelName","调度信息");
            newItem.put("attributeName", "调度信息");
            newItem.put("attributeValue", "司机姓名,司机手机号,车辆型号,车牌");
            newList.add(newItem);
        }else if(appId.equals("tv")||appId.equals("tvPack")){
            infoList.put("categoryId","1643");
            infoList.put("categoryName","液晶彩电");
            infoList.put("categoryLinkName","家用电器-液晶彩电");
            infoList.put("categoryFullName","家用电器-电视机-液晶彩电");
            infoList.put("categoryParentId","1640");
            infoList.put("categoryLevel",3);
            infoList.put("categoryCode","18009002");
            infoList.put("categoryCodeUseId",10);
            infoList.put("categoryTypeId",3);
            infoList.put("externalCode","10.18009002."+model);
            infoList.put("encodeTypeName","MA");

            infoList.put("modelName","电视机");
            newItem.put("attributeName", "电视信息");
            newItem.put("attributeValue", "产品型号,产品名称,USB2.0数量,USB3.0,HDMI2.1数量,HDMI2.0数量,液晶屏型号,产品价格");
            newList.add(newItem);
        }else if(appId.equals("lcd")||appId.equals("lcdPack")){
            infoList.put("categoryId","1646");
            infoList.put("categoryName","电视机配件");
            infoList.put("categoryLinkName","家用电器-电视机配件");
            infoList.put("categoryFullName","家用电器-电视机-电视机配件");
            infoList.put("categoryParentId","1640");
            infoList.put("categoryLevel",3);
            infoList.put("categoryCode","18009007");
            infoList.put("categoryCodeUseId",10);
            infoList.put("categoryTypeId",3);
            infoList.put("externalCode","10.18009007."+model);
            infoList.put("encodeTypeName","MA");

            infoList.put("modelName","液晶屏");
            newItem.put("attributeName", "液晶屏信息");
            newItem.put("attributeValue", "产品型号,分辨率,技术,刷新率,色域,视角,对比度");
            newList.add(newItem);
        }

        infoList.put("modelCode",model);
        infoList.put("details",newList);

        return infoList;
    }
    public static  String getNowTime(){
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formatDateTime = now.format(formatter);
        return formatDateTime;
    }
    public static  String getNowDate(){
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String formatDateTime = now.format(formatter);
        return formatDateTime;
    }
    public static  String getNowTimeStr(){
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        String formatDateTime = now.format(formatter);
        return formatDateTime;
    }
    public static  String getCode(String appId){
        String code="";
        if(appId.equals("etchant")){
            code="MA.156.110116.1007/10.18003000";
        }else if(appId.equals("etchmachine")){
            code="MA.156.110116.1007/10.18014000";
        }else if(appId.equals("pcbproducts")){
            code="MA.156.110116.1007/61.12000000";
        }else{
            code="MA.156.110116.1007/72.19000000";
        }
        return code;
    }
    public static boolean checkTime(String time){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 将字符串解析为 LocalDateTime 对象
        LocalDateTime dateTime = LocalDateTime.parse(time, formatter);
        // 从 LocalDateTime 对象中提取日期
        LocalDate inputDate = dateTime.toLocalDate();
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 判断输入日期是否为今天
        boolean isToday = inputDate.isEqual(currentDate);
        return isToday;
    }
    public static boolean checkDate(String time){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        // 将字符串解析为 LocalDate 对象
        LocalDate inputDate = LocalDate.parse(time, formatter);
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 判断输入日期是否为今天
        boolean isToday = inputDate.isEqual(currentDate);
        return isToday;
    }

    public static List<Map> compare(Object obj1, Object obj2) {
        try {
            Class<?> clazz = obj1.getClass();
            Field[] fields = clazz.getDeclaredFields();
            List<Map> list=new ArrayList<>();
            for (Field field : fields) {
                field.setAccessible(true);
                Object value1 = field.get(obj1);
                Object value2 = field.get(obj2);
                if (value1 == null && value2 == null) {
                    continue;
                } else if (value1 == null && value2 != null) {
                    //System.out.println("字段名：" + field.getName() + ", obj1: null, obj2: " + value2.toString());
                } else if (value1 != null && value2 == null) {
                    //System.out.println("字段名：" + field.getName() + ", obj1: " + value1.toString() + ", obj2: null");
                } else if (!value1.equals(value2)) {
                    //System.out.println("字段名：" + field.getName() + ", obj1: " + value1.toString() + ", obj2: " + value2.toString());
                    Map<String, Object> map = new HashMap<>();
                    map.put("field", field.getName());
                    map.put("value1", value1.toString());
                    map.put("value2", value2.toString());
                    list.add(map);
                }
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String postUrl(String url,Map<String,String> headerList,String jsonBody){
        HttpPost post = new HttpPost(url);
        post.setHeader("Content-Type", "application/json");
        if(headerList!=null){
            for (Map.Entry<String, String> entry : headerList.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                post.addHeader(key, value);
            }
        }
        try {
            // 设置请求体（JSON 格式）
            StringEntity entity = new StringEntity(jsonBody, "UTF-8");
            post.setEntity(entity);

            // 发送请求并获取响应
            try (CloseableHttpClient client = HttpClients.createDefault();
                 CloseableHttpResponse response = client.execute(post)) {

                // 获取响应体
                String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8");

                return responseBody;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    public static  String getUrl(String url,List<String> headerList) throws IOException {

        HttpGet get = new HttpGet(url);
        try (CloseableHttpResponse response = HttpClients.createDefault().execute(get)) {
            String json = EntityUtils.toString(response.getEntity(), "UTF-8");
            return json;
        }
    }

    public static  String putUrl(String url,Map<String,String> headerList) throws IOException {

        HttpPut put = new HttpPut(url);
        if(headerList!=null){
            for (Map.Entry<String, String> entry : headerList.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                put.addHeader(key, value);
            }
        }

        try (CloseableHttpClient client = HttpClients.createDefault()) {
            HttpResponse response = client.execute(put);
            String result = EntityUtils.toString(response.getEntity(), "UTF-8");
            return result;
        }
    }

}
